package com.qigao.mall.web.controller.admin.merchant;

import com.github.pagehelper.PageInfo;
import com.qigao.mall.api.SettleDetailService;
import com.qigao.mall.api.SettleTotalService;
import com.qigao.mall.commons.dto.SettleDetailResultDto;
import com.qigao.mall.commons.dto.SettleQueryDto;
import com.qigao.mall.commons.dto.SettleTotalResultDto;
import com.qigao.mall.commons.enums.Result;
import com.qigao.mall.commons.enums.ResultCode;
import com.qigao.mall.commons.utils.excel.ExcelFormat;
import com.qigao.mall.commons.utils.excel.ExcelHeaderInfo;
import com.qigao.mall.commons.utils.excel.ExcelUtils;
import com.qigao.mall.web.controller.AbstractDictController;
import com.qigao.mall.web.controller.AbstractExcelController;
import com.qigao.mall.web.vo.admin.merchant.SettleCondVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/settleDetail/")
@Api(tags = "Admin商户操作结算单明细相关API" ,value = "Admin商户操作结算单明细相关API")
public class AdminSettleDetailController extends AbstractExcelController {

    private static Logger logger= LoggerFactory.getLogger(AdminSettleDetailController.class);
    @Resource
    private SettleDetailService settleDetailService;

    @PostMapping("/listPage")
    @ApiOperation(value = "结算单明细查询列表", notes = "结算单明细查询列表")
    public Result<PageInfo<SettleDetailResultDto>> listPage(@RequestBody SettleCondVo settleCondVo, HttpServletRequest request){
        try{
            //参数校验
            SettleQueryDto settleQueryDto=new SettleQueryDto();
            BeanUtils.copyProperties(settleCondVo,settleQueryDto);
            if(StringUtils.isNotEmpty(settleCondVo.getDateBegin())){
                settleQueryDto.setDateBegin(settleCondVo.getDateBegin()+" 00:00:00");
            }
            if(StringUtils.isNotEmpty(settleCondVo.getDateEnd())){
                settleQueryDto.setDateEnd(settleCondVo.getDateEnd()+" 23:59:59");
            }
            return  settleDetailService.listPage(settleQueryDto);
        }catch (Exception e){
            logger.error("[按照结算单状态查询列表]发生异常:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }
    @GetMapping("/export")
    @ApiOperation(value = "导出Excel", notes = "导出Excel")
    public void exportGEt(SettleCondVo settleCondVo, HttpServletResponse response) {
        SettleQueryDto settleQueryDto=new SettleQueryDto();
        BeanUtils.copyProperties(settleCondVo,settleQueryDto);
        if(StringUtils.isNotEmpty(settleCondVo.getDateBegin())){
            settleQueryDto.setDateBegin(settleCondVo.getDateBegin()+" 00:00:00");
        }
        if(StringUtils.isNotEmpty(settleCondVo.getDateEnd())){
            settleQueryDto.setDateEnd(settleCondVo.getDateEnd()+" 23:59:59");
        }
        // 待导出数据
        List<SettleDetailResultDto> orderResultDtos = settleDetailService.findOrderResultByCond(settleQueryDto);
        ExcelUtils excelUtils = new ExcelUtils(orderResultDtos, getHeaderInfo(), getFormatInfo());
        sendHttpResponse(response, "settleDetailReport", excelUtils.getWorkbook());
    }


    @PostMapping("/export")
    @ApiOperation(value = "导出Excel", notes = "导出Excel")
    public void export(@RequestBody SettleCondVo settleCondVo, HttpServletResponse response) {
        SettleQueryDto settleQueryDto=new SettleQueryDto();
        BeanUtils.copyProperties(settleCondVo,settleQueryDto);
        if(StringUtils.isNotEmpty(settleCondVo.getDateBegin())){
            settleQueryDto.setDateBegin(settleCondVo.getDateBegin()+" 00:00:00");
        }
        if(StringUtils.isNotEmpty(settleCondVo.getDateEnd())){
            settleQueryDto.setDateEnd(settleCondVo.getDateEnd()+" 23:59:59");
        }
        // 待导出数据
        List<SettleDetailResultDto> orderResultDtos = settleDetailService.findOrderResultByCond(settleQueryDto);
        ExcelUtils excelUtils = new ExcelUtils(orderResultDtos, getHeaderInfo(), getFormatInfo());
        sendHttpResponse(response, "settleDetailReport", excelUtils.getWorkbook());
    }

    // 获取表头信息
    private List<ExcelHeaderInfo> getHeaderInfo() {
        return Arrays.asList(
                new ExcelHeaderInfo(0, 0, 0, 0, "结算日期"),
                new ExcelHeaderInfo(0, 0, 1, 1, "用户名称"),
                new ExcelHeaderInfo(0, 0, 2, 2, "分润金额"),
                new ExcelHeaderInfo(0, 0, 3, 3, "分红金额"),
                new ExcelHeaderInfo(0, 0, 4, 4, "待结算金额"),
                new ExcelHeaderInfo(0, 0, 5, 5, "所属公司")
        );
    }

    // 获取格式化信息
    private Map<String, ExcelFormat> getFormatInfo() {
        Map<String, ExcelFormat> format = new HashMap<>();
       /* format.put("id", ExcelFormat.FORMAT_INTEGER);
        format.put("categoryId", ExcelFormat.FORMAT_INTEGER);
    */
        return null;
    }


}
